const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    popupShow: true,
    goods_name: "",
    page: 1,
    finished: false,
    listData:[],
    explain:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getData();
  },
  getData () {
    if (this.data.finished) return;
    let baseUrl = app.getNetAddresss('plugin.stock-service.frontend.controllers.home.stock');
    app._getNetWork({
      url: baseUrl,
      data: {
        page: this.data.page,
        goods_name:this.data.goods_name
      },
      success: (respone) => {
        if (respone.data.result !== 1) {
          wx.showToast({
            title: respone.data.msg,
          });
          return false;
        }
        let res = respone.data.data;
        let finished = false;
        let listData = [];
        wx.setNavigationBarTitle({
          title: res.name,
        });
        this.setData({
          explain: res.explain
        });
        if (res.list.data.length === 0 || res.list.data.length < res.list.per_page || res.list.current_page === res.list.last_page) {
          finished = true;
        }
        listData = this.data.listData.concat(res.list.data);
        this.setData({
          finished,
          listData,
          page: ++this.data.page
        });
      }
    });
  },
  clsoePopup() {
    this.setData({
      popupShow: false
    })
  },
  onSearch(e) {
    this.setData({
      goods_name: e.detail,
      page: 1,
      finished: false,
      listData:[],
    });
    this.getData();
  },
  goGoodsDetail(e) {
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + e.currentTarget.dataset.id
    });
  }
})